package com.literature.threeGroup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.literature.threeGroup.mapper.LogMapper;
import com.literature.threeGroup.mapper.RoleUserMapper;
import com.literature.threeGroup.mapper.UserMapper;
import com.literature.threeGroup.mapper.UserVoMapper;
import com.literature.threeGroup.pojo.Log;
import com.literature.threeGroup.pojo.RoleUser;
import com.literature.threeGroup.pojo.User;
import com.literature.threeGroup.service.UserService;
import com.literature.threeGroup.util.IdWorker;
import com.literature.threeGroup.util.JwtUtil;
import com.literature.threeGroup.util.MD5Util;
import com.literature.threeGroup.util.PageResult;
import com.literature.threeGroup.vo.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author VULCAN
 * @create 2020-01-07 14:30
 * @desc
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserVoMapper userVoMapper;
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RoleUserMapper roleUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @name  用户登录
     * @Author VULCAN
     * @Date 2020/1/9 13:47
     */
    @Override
    public User userlogin(User user) throws UnknownHostException {

        //判断密码一不一致
        User userlogin = userMapper.userlogin(user.getUserName());
        if(userlogin!=null){
            if (MD5Util.MD5encode("threeGroup"+user.getUserPwd()).equals(userlogin.getUserPwd())){
                  Log log = new Log();
                  log.setLogId(idWorker.nextId()+"");
                  log.setLogDate(new Date());
                  log.setLogName(userlogin.getUserName());
                  InetAddress ip4 = Inet4Address.getLocalHost();
                  log.setLogIp(ip4.getHostAddress());
                  log.setLogContent("用户登录-成功！！");
                  log.setLogType("用户登录");
                  logMapper.insert(log);
                  redisTemplate.delete("login");
                  redisTemplate.opsForValue().set("login",user.getUserName());
                return userlogin;
            }
        }else{
            Log log = new Log();
            log.setLogId(idWorker.nextId()+"");
            log.setLogDate(new Date());
            log.setLogName(user.getUserName());
            InetAddress ip4 = Inet4Address.getLocalHost();
            log.setLogIp(ip4.getHostAddress());
            log.setLogContent("用户登录-失败！！");
            log.setLogType("用户登录");
            logMapper.insert(log);
        }
        //不一致
        return null;
    }
    /**
     * @name  更改登录时间
     * @Author VULCAN
     * @Date 2020/1/9 13:47
     */
    @Override
    public void updatelogindate(User user) {
        userMapper.updatelogindate(user);
    }

    /**
      * @name  用户管理
     * @Author VULCAN
     * @Date 2020/1/9 14:05
     */
    @Override
    public List<User> findall() {
        return userMapper.findall();
    }

    /**
      * @name  vo数据
     * @Author VULCAN
     * @Date 2020/1/9 20:44
     */
    @Override
    public List<UserRole> findByAll() {
        return userVoMapper.findByAll();
    }

    /**
      * @name  根据id查询
     * @Author VULCAN
     * @Date 2020/1/9 19:44
     */
    @Override
    public User findbyid(String userId) {
        return userMapper.findbyid(userId);
    }

    /**
      * @name  修改数据
     * @Author VULCAN
     * @Date 2020/1/9 19:44
     */
    @Override
    public void userupdate(User user) throws UnknownHostException {

        HttpServletRequest request = (
                (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()
        ).getRequest();

        userMapper.userupdate(user);
        //角色用户中间表
        RoleUser roleUser = new RoleUser();
        roleUser.setUserId(user.getUserId());
        roleUser.setRoleId(user.getUserAdmin());
        roleUserMapper.roleuserupdate(roleUser);
        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("用户修改-"+user.getUserName()+"！！");
        log.setLogType("用户修改");
        logMapper.insert(log);
    }

    /**
      * @name  添加数据
     * @Author VULCAN
     * @Date 2020/1/9 19:44
     */
    @Override
    public void userinsert(User user) throws UnknownHostException {

        HttpServletRequest request = (
                (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()
        ).getRequest();

        user.setUserId(idWorker.nextId()+"");
        //加密
        user.setUserPwd(MD5Util.MD5encode("threeGroup"+user.getUserPwd()));
        user.setUserLoginDate(new Date());
        user.setUserCdate(new Date());
        userMapper.insert(user);
        //角色用户中间表
        RoleUser roleUser = new RoleUser();
        roleUser.setUserId(user.getUserId());
        roleUser.setRoleId(user.getUserAdmin());
        roleUserMapper.insert(roleUser);
        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("用户添加-"+user.getUserName()+"！！");
        log.setLogType("用户添加");
        logMapper.insert(log);
    }
    /**
      * @name  修改密码
     * @Author VULCAN
     * @Date 2020/1/11 9:55
     */
    @Override
    public void userupdatepwd(User user) throws UnknownHostException {
        //加密
        user.setUserPwd(MD5Util.MD5encode("threeGroup"+user.getUserPwd()));
        userMapper.userupdatepwd(user);
        User findbyid = userMapper.findbyid(user.getUserId());
        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("用户修改密码-"+findbyid.getUserName()+"！！");
        log.setLogType("用户修改密码");
        logMapper.insert(log);
    }
    /**
     * @name  分页模糊
     * @Author VULCAN
     * @Date 2020/1/17 9:45
     */
    @Override
    public PageResult findByAllname(UserRole userRole, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<UserRole> findlog = (Page<UserRole>) userVoMapper.findByAllname(userRole);
        return new PageResult(findlog.getTotal(),findlog.getResult());
    }
    /**
     * @name  批量删除
     * @Author VULCAN
     * @Date 2020/1/17 9:47
     */
    public String userbulkdel(String[] ids) throws UnknownHostException {
        ArrayList<Object> list = new ArrayList<>();
        for (String id : ids) {
            list.add(id);
            userMapper.userdel(id+"");
        }

        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("用户批量删除-"+list+"！！");
        log.setLogType("用户批量删除");
        logMapper.insert(log);
        return null;
    }
    /**
     * @name  usercount
     * @Author VULCAN
     * @Date 2020/1/11 10:01
     */
    @Override
    public List usercount() {
        return userMapper.usercount();
    }
}
